
<template>
  <div class="page">
    <div class="layui-fluid">
      <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
          <div class="layui-card">
            <div class="layui-card-body cardbody">
              <div class="layui-form" lay-filter="">

                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label">会员等级</label>
                    <div class="layui-input-inline"> 
                      <input type="text" name="levelName" placeholder="请输入会员等级" autocomplete="off" class="layui-input" disabled='disabled'
                        v-model="itemData.levelMarketingDTOList[0].levelName">
                    </div>
                  </div>
                </div>

                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label">有效期时间</label>
                    <div class="layui-input-inline">
                      <input type="number" name="expirationDay" placeholder="请输入时间" autocomplete="off"
                        v-model="itemData.expirationDay" class="layui-input" />
                    </div>
                  </div>
                  <span class="notes">单位:月(至少一个月)</span>
                </div>

                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label">优惠券日期</label>
                    <div class="layui-input-inline">
                      <input type="number" name="couponDay" placeholder="请输入日期" autocomplete="off"
                        v-model="itemData.couponDay" class="layui-input" />
                    </div>
                  </div>
                  <span class="notes">到账日期(每月几号1号就传1)</span>
                </div>

                <div class="layui-form-item">
                  <label class="layui-form-label">手机号</label>
                  <div class="layui-input-inline">
                    <select name="mobileStatus" lay-filter="mobileStatus" placeholder="手机号是否必填"
                      v-model="itemData.mobileStatus">
                      <option value="YES">是</option>
                      <option value="NO">否</option>
                    </select>
                  </div>
                  <span class="notes">手机号是否必填</span>
                </div>

                <div class="layui-form-item">
                  <label class="layui-form-label">优惠券名称</label>
                  <div class="layui-input-inline">
                    <select name="publishId" lay-filter="publishId"
                      v-model="itemData.levelMarketingDTOList[0].publishId">
                      <option v-for="item in yhjlist" :value="item.id">{{item.name}}</option>
                    </select>
                  </div>
                </div>

                <div class="layui-form-item">
                  <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-normal" @click="sure">
                      确认
                    </button>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "memberyx",
  data () {
    return {
      id: "",
      compangId: "",
      dealerId: "",
      status: "",
      itemData: {
        mobileStatus: "", //手机号
        expirationDay: "", //有效期时间
        couponDay: "", //优惠券自动到账日期
        levelMarketingDTOList: [
          {
            levelId: "",//会员等级id
            levelName: "",//会员等级名称
            number: "1",
            publishId: ""//优惠券id
          }
        ],
      },

      yhjlist: [],//优惠券名称
      djlist: [],//等级
      ppid: "",
      type: localStorage.getItem("type")
    };
  },
  mounted () {
    var _this = this;
    this.getyhj();//优惠券
    this.getdj();//等级
    window.getdates = this.getdates;
  },
  methods: {
    getdates: function (data) {
      var vm = this;
      vm.id = data.id;
      var url = "/crm/aapi/v1.9/member-marketing/level/" + data.id;
      ajaxRequest(url, "GET", "json", "", function (ret, err) {
        if(ret.data){
          if (ret.data.levelMarketingDTOList != null) {
          vm.itemData = ret.data;
          vm.ppid = ret.data.id;
        } else {
          vm.itemData.couponDay = ret.data.couponDay;
          vm.itemData.expirationDay = ret.data.expirationDay;
          vm.itemData.mobileStatus = ret.data.mobileStatus;
          vm.ppid = ret.data.id;
        }
        }
        


      });
    },
    getdj: function () {
      var vm = this;
      var url = "/shop/aapi/v1.9/levels/"+  this.$route.query.id;
      ajaxRequest(url, "GET", "json", "", function (ret, err) {
        vm.itemData.levelMarketingDTOList[0].levelId = ret.data.id
        vm.itemData.levelMarketingDTOList[0].levelName = ret.data.name
      });
    },
    getyhj: function () {
      var vm = this;
      var url = "/crm/papi/v1.9/coupon/admin/template" + "?size=10000";
      ajaxRequest(url, "GET", "json", "", function (ret, err) {
        vm.yhjlist = ret.data.list;
        // alert(JSON.stringify(ret.data))
      });
    },
    sure: function () {
      var _this = this;
      var tid = 'POST'
      if (_this.itemData.mobileStatus == "NO") {
        layer.msg("请先开启手机号");
        return false;
      }
      var dates = _this.itemData;
      if (_this.ppid) {
        dates.id = _this.ppid;
        var tid = 'PUT';
      }
      if (_this.itemData.levelMarketingDTOList[0].number != "1") {
        _this.itemData.levelMarketingDTOList[0].number = "1"
      }
      // alert(JSON.stringify(dates));
      var url = "/crm/aapi/v1.9/member-marketing";
      ajaxRequest(url, tid, "json", dates, function(ret, err) {
        // alert(JSON.stringify(ret.dates));
        layer.msg("操作成功");
        setTimeout(function () {
          var index = parent.layer.getFrameIndex(window.name);
          parent.layer.close(index);
          parent.location.reload();
        }, 1000);
      });
    }
  },
  updated: function () {
    var _this = this;
    layui.use("form", function () {
      var form = layui.form;
      form.render();
      form.on("select(mobileStatus)", function (data) {
        _this.itemData.mobileStatus = data.value;
      });
      form.on("select(publishId)", function (data) {
        _this.itemData.levelMarketingDTOList[0].publishId = data.value;
      });
    });
  }
};
</script>
<style scoped>
.notes {
  color: #999;
}
</style>
